// pages/login/login.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp()
import {
    LoginRequest, // 登录
    GetStudentinfoAdd, // 更新用户头像昵称
    GetByOpenId, // 查询用户信息
} from '../../api/index'
  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    headUrl:'',
    nickname:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onChange(event) {
      console.log(event)
    this.setData({
      checked: event.detail,
    });
  },


//   onGotUserInfo: function(e) {
//     console.log(e.detail.errMsg)
//     console.log(e.detail.userInfo)
//     console.log(e.detail.rawData)
//   },
//   bindGetUserInfo: function(e) {
//     console.log('回调成功')
//     console.log(e)
//   },

  // 获取用户信息
  getUserInfo: function (e) {//点击的“拒绝”或者“允许
    console.log(e)
    
    let that= this;
    if (that.data.checked == false) {
        Toast('请阅读并同意用户服务协议以及隐私协议')
        return
    }
    wx.getUserProfile({
      desc: "获取你的昵称、头像、地区及性别",
      success: res => {
        console.log(res)
        console.log(res)
        that.setData({
          headUrl:res.userInfo.avatarUrl,
          nickname:res.userInfo.nickName
        })
        // that.loginAction()
      },
      fail: res => {
         //拒绝授权
        that.showErrorModal('您拒绝了请求');
        return;
      }
    })
  },

  // 用户协议
  userAction() {
    wx.navigateTo({
        url : '/pages/mine/rich/rich?type=2'
    })
  },

  // 登录
  loginAction() {
    if (this.data.checked == false) {
        Toast('请阅读并同意用户服务协议以及隐私协议')
        return
    }
    let that = this;
        wx.login({
          success: function(data) {
            console.log(data)
            console.log({appcode:data.code})
            that._GetStudentinfoAdd({openId:data.code,headPortrait:that.data.headUrl,reserve1:that.data.nickname},false)
            
            that._LoginRequest({appcode:data.code}, true);
          },
          fail: function(err) {
            wx.showToast({
                title: '登录失败',
                icon:"none",
            })
            console.log('wx.login 接口调用失败，将无法正常使用开放接口等服务', err)
            callback(err)
          }
        })
  },

  
    /** 更新用户头像昵称 */
    _GetStudentinfoAdd(data, isLoading) {
        console.log(data)
        GetStudentinfoAdd(data, isLoading).then(res => {
          console.log(res)
          
        })
      },

  /** 登录 */
  _LoginRequest(data, isLoading) {
    console.log(data)
    LoginRequest(data, isLoading).then(res => {
      console.log(res)
      let code = res.code;
      if (code == 200) {
        wx.setStorageSync('headimg', res.headimg);
        wx.setStorageSync('nickname', res.nickname);
        wx.setStorageSync('openId', res.openid);
        wx.setStorageSync('token', res.token);    
        this._GetByOpenId({openId:res.openid}, false)    
        wx.switchTab({
          url: '/pages/index/index',
        })
      } else {
        Toast(res.msg)
      }
    })
  },

  _GetByOpenId(data, isLoading) {
    console.log('激活')
    GetByOpenId(data, isLoading).then(res => {
        console.log('激活')
        console.log(res)
        let that = this;
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            console.log(res.data.status)
            app.globalData.openVip = res.data.status;
            console.log(app.globalData.openVip)
        } else {
            Toast(res.msg)
        }
    })
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  
})